package com.tyl;

public class ResultEntity<T> {
    private int code;
    private String message;
    private T data;

    public long getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }


    private ResultEntity(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static ResultEntityBuilder builder(int code,String msg){
        return new ResultEntityBuilder(code,msg);
    }

    public static <T> ResultEntity<T> ok(){
        return ok(null);
    }

    public static <T> ResultEntity<T> ok(T data){
        return builder(1,"success").data(data);
    }


    public static <T> ResultEntity<T> error(){
        return error("error");
    }

    public static <T> ResultEntity<T> error(String msg){
        return builder(0,msg).data();
    }

    @Override
    public String toString() {
        return "ResultEntity{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    public static class ResultEntityBuilder{
        private final int code;
        private  String msg;

        private ResultEntityBuilder(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public ResultEntityBuilder msg(String msg){
            this.msg = msg;
            return this;
        }

        public <T> ResultEntity<T> data(){
            return data(null);
        }
        public <T> ResultEntity<T> data(T data){
            return new ResultEntity<>(this.code,this.msg,data);
        }
    }

}
